package com.darrenchan.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-28 18:32
 */
public class Q589 {
    public List<Integer> preorder(Node root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }

        Stack<Node> stack = new Stack<>();
        stack.add(root);

        while (!stack.isEmpty()) {
            root = stack.pop();
            list.add(root.val);
            int len = root.children.size();
            for (int i = len - 1; i >= 0; i--) {
                Node node = root.children.get(i);
                if(node != null){
                    stack.add(node);
                }
            }
        }
        return list;
    }
}
